package com.example.zhaoping.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.zhaoping.entity.Menu;
import com.example.zhaoping.serviceImpl.MenuServiceImpl;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.awt.*;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List;

@Component
@Order
public class GetUrl implements ApplicationRunner {

    @Autowired
    private WebApplicationContext applicationContext;
    @Resource
    private MenuServiceImpl menuService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : map.keySet()) {
            Set<String> urlstr = info.getDirectPaths();
            String name = info.getName();
            String url = (String) urlstr.toArray()[0];
            Menu menu = new Menu();
            Menu menu1 = new Menu();
            if(!url.equals("/error")){
                menu.setName(name);
                menu.setUrl(url);
                menu1 = menuService.selectByUrl(url);
                if(menu1 == null){
                    int inster_menu = menuService.inster(menu);
                }
            }
        }
    }

}
